<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class AccessReddit extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'access:reddit';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $client = new Client();

        $response = $client->post('https://www.reddit.com/api/v1/access_token', [
            'auth' => [env('REDDIT_CLIENT_ID'), env('REDDIT_SECRET')],
            'form_params' => [
                'grant_type' => 'password',
                'username' => env('REDDIT_USERNAME'),
                'password' => env('REDDIT_PASSWORD'),
            ],
            'headers' => [
                'User-Agent' => 'MyRedditApp/0.1 by ' . env('REDDIT_USERNAME'),
            ]
        ]);


        $data = json_decode((string) $response->getBody(), true);

        dd($data);

        $this->info('Access Token: ' . $data['access_token']);
    }
}
